pub use sea_orm::entity::prelude::*;

use crate::{booking_status, booking};

#[derive(Debug, Clone, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "view_custom")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub uid: i64,
    pub user_name: String,
    pub pwd: String,
}

#[derive(Debug, Clone, Copy, EnumIter)]
pub enum Relation {}

impl RelationTrait for Relation {
    fn def(&self) -> RelationDef {
        panic!("No Relate")
    }
}

impl Related<booking_status::Entity> for Entity {
    fn to() -> RelationDef {
        booking::Relation::BookingStatus.def()
    }
    fn via() -> Option<RelationDef> {
        booking::Relation::ViewCustom.def().rev().into()
    }
}

impl ActiveModelBehavior for ActiveModel {}

#[cfg(test)]
mod test_custom_view {

    use tokio::test;

    use super::*;
    use crate::utils::test_conn::get_connection;

    #[test]
    async fn test_exist_user_login() {
        let db = get_connection().await;

        let user = Entity::find()
            .filter(Column::UserName.eq("custom"))
            .one(db)
            .await
            .expect("query error")
            .expect("user not exist");

        assert_eq!(
            bcrypt::verify("123456", &user.pwd).expect("bcrypt err"),
            true
        )
    }
    #[test]
    async fn test_user_no_exist_login() {
        let db = get_connection().await;

        let resp = Entity::find()
            .filter(Column::UserName.eq("abc"))
            .one(db)
            .await
            .expect("query error");

        assert_eq!(resp, None);
    }
}
